package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.PurchaseItem;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.mapper.PurchaseItemMapper;
import com.woniuxy.factory.mapper.PurchaseRequestMapper;
import com.woniuxy.factory.service.PurchaseItemService;
import com.woniuxy.factory.util.AuditType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 邹佳骏
 * @date 2020-08-17 21:08
 */
@Service
public class PurchaseItemServiceImp implements PurchaseItemService {

    @Autowired
    PurchaseItemMapper purchaseItemMapper;

    @Autowired
    PurchaseRequestMapper purchaseRequestMapper;

    @Override
    public List<PurchaseItem> selectByPurid(String purId, int page, int limit) {

        int home = (page-1)*limit;
        List<PurchaseItem> purchaseItems = purchaseItemMapper.selectByPurid(purId, home, limit);

        return purchaseItems;
    }

    @Override
    public int getCount(String purId) {

        int allcount = purchaseItemMapper.getCount(purId);

        return allcount;
    }

    @Override
    public ResponseResult deleteByPuiId(String puiID) {
        ResponseResult responseResult = new ResponseResult();

        //捕捉异常，出现异常响应不同的信息
        try {
            purchaseItemMapper.deleteByPuiId(puiID);
        }catch (RuntimeException e){
            responseResult.setMsg("删除数据异常");
            return responseResult;
        }
        responseResult.setMsg("删除成功");
        responseResult.setCode(AuditType.No_Approval);
        return responseResult;
    }

    @Override
    public ResponseResult insertItemByRequestNumber(String requestnumber, String matId, int purchaseamount) {

        ResponseResult responseResult = new ResponseResult();

        /*根据采购编号查询采购序号*/
        int purId = purchaseRequestMapper.selectpurIdByRequestnumber(requestnumber);

        try {
            /*插入订单项数据*/
            purchaseItemMapper.insertItemByRequestNumber(purId,matId,purchaseamount);
        }catch (RuntimeException e){
            responseResult.setMsg("插入数据失败");
            return responseResult;
        }
        responseResult.setMsg("添加成功");
        responseResult.setCode(AuditType.No_Approval);
        return responseResult;
    }
}